package com.koron.car.web.lend.service.impl;

import com.koron.bean.system.staff.vo.EamUser;
import com.koron.car.config.Constants;
import com.koron.car.web.carledger.service.CarService;
import com.koron.car.web.common.CommonUtils;
import com.koron.car.web.lend.bean.entity.CarLend;
import com.koron.car.web.lend.bean.entity.CarLendItem;
import com.koron.car.web.lend.bean.query.CarLendQuery;
import com.koron.car.web.lend.bean.vo.CarLendVO;
import com.koron.car.web.lend.mapper.CarLendMapper;
import com.koron.car.web.lend.service.CarLendItemService;
import com.koron.car.web.lend.service.CarLendService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (CarLend)表服务实现类
 *
 * @author twcao
 * @since 2022-03-10 16:27:53
 */
@Service
public class CarLendServiceImpl implements CarLendService {

    @Autowired
    private CarLendItemService itemService;

    @Autowired
    private CarService carService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public CarLendVO queryById(SessionFactory factory, String id) {
        CarLendMapper mapper = factory.getMapper(CarLendMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     * @param query 查询起始位置
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<CarLend> queryList(SessionFactory factory, CarLendQuery query) {
        CarLendMapper mapper = factory.getMapper(CarLendMapper.class);
        return mapper.queryList(query);
    }

    /**
     * 新增数据
     * @param lend 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public CarLendVO insert(SessionFactory factory, CarLendVO lend) {
        CarLendMapper mapper = factory.getMapper(CarLendMapper.class);
        Assert.notNull(lend, "借出信息不能为空");
        EamUser eamUser = ThreadLocalContext.get();
        CommonUtils.update(lend, eamUser);
        lend.setDeptId(eamUser.getOrgId());
        lend.setId(CodeTools.getCode32());
        lend.setState(Constants.LendState.DRAFT);
        mapper.insert(lend);
        List<CarLendItem> items = lend.getItems();
        CommonUtils.sorted(items);
        items.stream().forEach(row -> row.setLendId(lend.getId()));
        itemService.insertBatch(factory, items);
        return lend;
    }

    /**
     * 修改数据
     * @param lend 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public CarLendVO update(SessionFactory factory, CarLendVO lend) {
        CarLendMapper mapper = factory.getMapper(CarLendMapper.class);
        Assert.hasText(lend.getId(), "id不能为空");
        CarLendVO db = mapper.queryById(lend.getId());
        lend.setState(db.getState());
        boolean submit = lend.getSubmit();
        if(submit) {
            // 提交前的状态
            int oldIdx = Constants.LendState.stateList.indexOf(db.getState());
            Assert.isTrue(oldIdx < Constants.LendState.stateList.size() - 1, "流程已完成");
            // 提交后的状态
            String state = Constants.LendState.stateList.get(++oldIdx);
            lend.setState(state);
        }
        mapper.update(lend);
        CommonUtils.update(lend, ThreadLocalContext.get());
        itemService.deleteByLendId(factory, lend.getId());
        List<CarLendItem> items = lend.getItems();
        CommonUtils.sorted(items);
        items.stream().forEach(row -> row.setLendId(lend.getId()));
        itemService.insertBatch(factory, items);
        switch (lend.getState()) {
            case Constants.LendState.BORROWED:
                carService.updateState(factory, lend.getCarId(), Constants.CarState.LEND);
                break;
            case Constants.LendState.FINISH:
                carService.updateState(factory, lend.getCarId(), Constants.CarState.FREE);
                break;
            default:
                break;
        }
        return this.queryById(factory, lend.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        CarLendMapper mapper = factory.getMapper(CarLendMapper.class);
        return mapper.deleteById(id) > 0;
    }
}
